from flask import Flask, request, jsonify
import requests

app = Flask(__name__)
DEEPSEEK_URL = "https://api.deepseek.com/v1/chat/completions"
API_KEY = "sk-d40af8f1b6f54df89b9209140694333e"  # 替换为你的API密钥

@app.route('/')
def index():
    return '''
    <!DOCTYPE html>
    <html>
    <head>
        <title>DeepSeek Chat</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            :root {
                --primary-color: #4a90e2;
                --secondary-color: #f5f5f5;
                --text-color: #333;
                --border-color: #e0e0e0;
                --user-message-bg: #e3f2fd;
                --bot-message-bg: #f5f5f5;
                --gradient-start: #6a11cb;
                --gradient-end: #2575fc;
            }

            * {
                margin: 0;
                padding: 0;
                box-sizing: border-box;
                font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            }

            body {
                background: linear-gradient(135deg, var(--gradient-start), var(--gradient-end));
                color: var(--text-color);
                line-height: 1.6;
                min-height: 100vh;
            }

            .container {
                max-width: 800px;
                margin: 0 auto;
                padding: 20px;
                height: 100vh;
                display: flex;
                flex-direction: column;
            }

            .header {
                text-align: center;
                padding: 20px 0;
                margin-bottom: 20px;
                animation: slideDown 0.5s ease;
            }

            @keyframes slideDown {
                from { transform: translateY(-50px); opacity: 0; }
                to { transform: translateY(0); opacity: 1; }
            }

            .header h1 {
                color: white;
                font-size: 2.5em;
                margin-bottom: 10px;
                text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
            }

            .header p {
                color: rgba(255,255,255,0.8);
                font-size: 1.2em;
            }

            #chatbox {
                flex: 1;
                overflow-y: auto;
                padding: 20px;
                background: rgba(255,255,255,0.95);
                border-radius: 15px;
                box-shadow: 0 4px 20px rgba(0,0,0,0.1);
                margin-bottom: 20px;
                backdrop-filter: blur(10px);
            }

            .message {
                margin: 15px 0;
                padding: 12px 16px;
                border-radius: 12px;
                max-width: 80%;
                position: relative;
                animation: fadeIn 0.3s ease;
                box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            }

            @keyframes fadeIn {
                from { opacity: 0; transform: translateY(10px); }
                to { opacity: 1; transform: translateY(0); }
            }

            .user {
                background: linear-gradient(135deg, #4a90e2, #357abd);
                color: white;
                margin-left: auto;
                border-bottom-right-radius: 4px;
            }

            .bot {
                background: white;
                margin-right: auto;
                border-bottom-left-radius: 4px;
                border: 1px solid var(--border-color);
            }

            .input-container {
                display: flex;
                gap: 10px;
                padding: 15px;
                background: rgba(255,255,255,0.95);
                border-radius: 15px;
                box-shadow: 0 4px 20px rgba(0,0,0,0.1);
                backdrop-filter: blur(10px);
                animation: slideUp 0.5s ease;
            }

            @keyframes slideUp {
                from { transform: translateY(50px); opacity: 0; }
                to { transform: translateY(0); opacity: 1; }
            }

            input {
                flex: 1;
                padding: 12px 20px;
                border: 2px solid transparent;
                border-radius: 25px;
                outline: none;
                font-size: 16px;
                transition: all 0.3s;
                background: rgba(255,255,255,0.9);
            }

            input:focus {
                border-color: var(--primary-color);
                box-shadow: 0 0 10px rgba(74,144,226,0.3);
            }

            button {
                padding: 12px 24px;
                background: linear-gradient(135deg, var(--gradient-start), var(--gradient-end));
                color: white;
                border: none;
                border-radius: 25px;
                cursor: pointer;
                font-size: 16px;
                transition: all 0.3s;
                box-shadow: 0 4px 15px rgba(0,0,0,0.1);
            }

            button:hover {
                transform: translateY(-2px);
                box-shadow: 0 6px 20px rgba(0,0,0,0.15);
            }

            button:active {
                transform: translateY(0);
                box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            }

            /* 滚动条样式 */
            #chatbox::-webkit-scrollbar {
                width: 8px;
            }

            #chatbox::-webkit-scrollbar-track {
                background: rgba(255,255,255,0.1);
                border-radius: 4px;
            }

            #chatbox::-webkit-scrollbar-thumb {
                background: rgba(74,144,226,0.5);
                border-radius: 4px;
            }

            #chatbox::-webkit-scrollbar-thumb:hover {
                background: rgba(74,144,226,0.7);
            }

            /* 打字机效果 */
            .typing {
                display: inline-block;
                width: 10px;
                height: 10px;
                background: var(--primary-color);
                border-radius: 50%;
                margin-left: 5px;
                animation: typing 1s infinite;
            }

            @keyframes typing {
                0%, 100% { transform: translateY(0); }
                50% { transform: translateY(-5px); }
            }

            /* 响应式设计 */
            @media (max-width: 600px) {
                .container {
                    padding: 10px;
                }
                
                .message {
                    max-width: 90%;
                }
                
                button {
                    padding: 12px 16px;
                }

                .header h1 {
                    font-size: 2em;
                }
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="header">
                <h1>DeepSeek Chat</h1>
                <p>智能对话助手</p>
            </div>
            <div id="chatbox"></div>
            <div class="input-container">
                <input type="text" id="input" placeholder="输入消息..." autocomplete="off">
                <button onclick="sendMessage()">发送</button>
            </div>
        </div>
        <script>
            function addMessage(role, content) {
                const chatbox = document.getElementById('chatbox');
                const div = document.createElement('div');
                div.className = `message ${role}`;
                div.innerHTML = content;
                chatbox.appendChild(div);
                chatbox.scrollTop = chatbox.scrollHeight;
            }

            async function sendMessage() {
                const input = document.getElementById('input');
                const message = input.value.trim();
                if (!message) return;
                
                addMessage('user', message);
                input.value = '';
                
                // 添加打字机效果
                const typingIndicator = document.createElement('div');
                typingIndicator.className = 'message bot';
                typingIndicator.innerHTML = '<span class="typing"></span><span class="typing"></span><span class="typing"></span>';
                document.getElementById('chatbox').appendChild(typingIndicator);
                
                try {
                    const response = await fetch('/chat', {
                        method: 'POST',
                        headers: { 'Content-Type': 'application/json' },
                        body: JSON.stringify({ messages: [{ role: 'user', content: message }] })
                    });
                    
                    const data = await response.json();
                    typingIndicator.remove();
                    addMessage('bot', data.choices[0].message.content);
                } catch (error) {
                    typingIndicator.remove();
                    addMessage('bot', '出错啦: ' + error.message);
                }
            }

            // 添加回车发送功能
            document.getElementById('input').addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    sendMessage();
                }
            });

            // 添加输入框自动聚焦
            document.addEventListener('DOMContentLoaded', function() {
                document.getElementById('input').focus();
            });
        </script>
    </body>
    </html>
    '''

@app.route('/chat', methods=['POST'])
def chat():
    data = {
        "model": "deepseek-chat",
        "messages": request.json['messages']
    }
    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }
    response = requests.post(DEEPSEEK_URL, json=data, headers=headers)
    return jsonify(response.json())

if __name__ == '__main__':
    app.run()